package com.qf.mybatisplus.controller.rabbitMQ;

import com.qf.mybatisplus.config.rabbitMQConfig.RabbitMQCommonConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;
import java.util.UUID;

@Controller
@RequestMapping("/publisher")
public class PublisherController {


    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
    * @Description Hello-World
    * @Param []
    * @Author 12136
    * @Date 13:37 2021/11/24
    * @return void
    **/
    @RequestMapping("/send")
    void test1() throws IOException {
        //Hello-World 模式

        // 方式1  在配置类中  创建  queue
        // rabbitTemplate.convertAndSend("simpleQueue", "Hello world222222!! "+ System.currentTimeMillis());

        // 方式2  在注解上  创建 queue
        for (int i = 0; i < 50; i++) {
            rabbitTemplate.convertAndSend("helloQueue", "Hello world111111!! "+ System.currentTimeMillis());
            System.out.println("消息已发送");
        }

        // 便于观察效果, 加入下面的代码
        new Scanner(System.in).nextLine(); // 输入任何字符或回车结束程序
    }

    /**
    * @Description Work 模型
    * @Param []
    * @Author 12136
    * @Date 13:37 2021/11/24
    * @return void
    **/
    @RequestMapping(value = "/work",method = RequestMethod.POST)
    public void testWork(){

        // Work 模型
        for(int i=0;i<50;i++){

            rabbitTemplate.convertAndSend("work","work模型:   "+i);
        }
    }

    /**
    * @Description Pub/Sub  生产发布模型   广播模型
    * @Param []
    * @Author 12136
    * @Date 13:59 2021/11/24
    * @return void
    **/
    @RequestMapping(value = "/fanout",method = RequestMethod.POST)
    public void testFanout(){
        for(int i=0;i<50;i++){

            // 生产发布模型   广播模型
            rabbitTemplate.convertAndSend("boot-pubsub-exchange","","广播模式");
        }

    }

    /**
    * @Description route  路由模式
    * @Param []
    * @Author 12136
    * @Date 14:07 2021/11/24
    * @return void
    **/
    @RequestMapping(value = "/route",method = RequestMethod.POST)
    public void testRouting(){
        // 路由模式
//        rabbitTemplate.convertAndSend("boot-route-exchange","info","发送的是info的key的路由信息");
        rabbitTemplate.convertAndSend("boot-route-exchange","error","发送的是error的key的路由信息");
    }
    
    /**
    * @Description topic
    * @Param []
    * @Author 12136
    * @Date 14:16 2021/11/24
    * @return void
    **/
    @RequestMapping(value = "/topic",method = RequestMethod.POST)
    void testTopic()
    {
        // Topic 模式   基于注解
        //rabbitTemplate.convertAndSend("boot-topic-exchange","slow.red.dog","红色大狼狗！！");
        rabbitTemplate.convertAndSend("boot-topic-exchange","black.dog.and.cat","黑色狗和猫！！");
    }

    /**
    * @Description 避免消息重复消费
    * @Param []
    * @Author 12136
    * @Date 14:28 2021/11/24
    * @return void
    **/
    @RequestMapping(value = "/repeatTopic",method = RequestMethod.POST)
    void testRepeat(){
        CorrelationData messageId = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("boot-topic-exchange-repeat","black.dog.and.cat","黑色狗和猫！！",messageId);
        //System.in.read();
    }

    /**
    * @Description 发送带有过期时间的消息，消息过期后进入死信队列
    * @Param []
    * @Author 12136
    * @Date 16:23 2021/11/24
    * @return void
    **/
    @RequestMapping(value = "/sendTTLMessage",method = RequestMethod.POST)
    public void sendTTLMessage(){

        String msg = "测试设置了过期时间的消息"+new Date().toLocaleString();
        MessageProperties messageProperties=new MessageProperties();   // 消息属性对象
        messageProperties.setMessageId(UUID.randomUUID().toString());

        messageProperties.setExpiration("10000");  // 设置消息的过期时间为10秒

        Message message = new Message(msg.getBytes(),messageProperties);

        rabbitTemplate.send(RabbitMQCommonConfig.TTL_DIRECT_EXCHANGE,RabbitMQCommonConfig.TTL_DIRECT_ROUTINGKEY,message);
    }

}
